package cn.zebra.dev.shop.module.order.controller;

import cn.zebra.dev.common.restful.Result;
import cn.zebra.dev.ibatis.plus.utils.PageUtils;
import cn.zebra.dev.order.module.item.entity.OrdersItemEntity;
import cn.zebra.dev.order.module.item.service.OrdersItemService;
import cn.zebra.dev.order.module.order.entity.OrdersEntity;
import cn.zebra.dev.order.module.order.service.OrdersService;
import cn.zebra.dev.shop.module.order.dto.OrderDetailsDTO;
import cn.zebra.dev.shop.module.order.dto.OrdersDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "店铺订单管理")
@RequestMapping("/shopOrder")
public class ShopOrderController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private  OrdersItemService ordersItemService;

    @GetMapping("/list/{shopId}")
    @ApiOperation("店铺订单列表")
    public Result<PageUtils<OrdersDTO>> list(
            @PathVariable Long shopId,
            @RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit,
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page) {
        Map<String, Object> params = new HashMap<>();
        params.put("shopId",shopId);
        params.put("limit", limit);
        params.put("page", page);
        PageUtils<OrdersEntity> pageUtils = ordersService.findPageByShopId(params);
        PageUtils<OrdersDTO> data = new PageUtils<>(pageUtils);
        List<OrdersEntity> list = pageUtils.getList();
        if (list.size() > 0) {
            List<OrdersDTO> dtoList = new LinkedList<>();
            list.forEach(entity -> {
                OrdersDTO dto = new OrdersDTO();
                BeanUtils.copyProperties(entity, dto);
                dtoList.add(dto);
            });
            data.setList(dtoList);
        }
        return Result.success(data);
    }

    @GetMapping("/get/{orderId}")
    @ApiOperation("查看订单详情")
    public Result<OrderDetailsDTO> get(@PathVariable Long orderId) {
        OrderDetailsDTO dto = new OrderDetailsDTO();
        OrdersEntity entity = ordersService.findById(orderId);
        BeanUtils.copyProperties(entity,dto);
        List<OrdersItemEntity> ordersItemEntities = ordersItemService.selectList("orders_id = {0}",orderId);
        dto.setOrdersItemEntities(ordersItemEntities);
        return Result.success(dto);
    }












}
